package pathfind;

import data.NavmeshRegion;
import java.awt.Point;
import java.util.*;

/**
 * Pathfinder that try to search path in particular region
 */
public class LocalPathFinder extends AStarAbstract<Point> {

    private final Point end;
    private final NavmeshRegion polygon;

    public LocalPathFinder(Point end, NavmeshRegion polygon) {
        this.end = end;
        this.polygon = polygon;
        additionalParam = polygon;
    }

    @Override
    protected boolean isGoal(Point node) {
        return node.equals(end);
    }

    @Override
    protected double g(Point from, Point to) {
        if (from == to) {
            return 0.0;
        }

        return squaredLength(from, to);
    }

    @Override
    protected double h(Point from, Point to) {
        return Math.abs(from.x - to.x) + Math.abs(from.y - to.y);
    }

    private double squaredLength(Point p1, Point p2) {
        double a = p1.x - p2.x;
        a = a * a;
        double b = p1.y - p2.y;
        b = b * b;
        return a + b;
    }

    @Override
    protected List<Point> generateSuccessors(Point node, List<Point> successorsOutputList) {
        return polygon.getNeighbours(node, successorsOutputList);
    }

    @Override
    protected double squaredLengthToGoal(Point cell) {
        double a = end.x - cell.x;
        a = a * a;
        double b = end.y - cell.y;
        b = b * b;
        return a + b;
    }

    @Override
    public AStarAbstract<Point> newInstance(Point end, Object additionalParam) {
        return new LocalPathFinder(end, (NavmeshRegion) additionalParam);
    }
}